rm(list = ls())
here::i_am(file.path("code", "18_spillovers.R"))
library(here)
source(here("code", "config.R"))

baseline_match_type <- "standard_match_0"

# Want to make analysis dataset with all census individuals

cards <- read_parquet(here("data", "analysis", "cards.parquet"))
census <- read_parquet(here("data", "analysis", "census.parquet"))
matches <- read_parquet(
  here("data", "analysis", "cards_census_matches.parquet"),
  col_select = c("card_id", "standard_match_0")
) |>
  rename(census_id = standard_match_0)

est_df <- cards |>
  inner_join(matches, by = "card_id") |>
  right_join(census, by = "census_id") |>
  mutate(vet_combined = as.integer(replace_na(vet_cards, FALSE) | replace_na(vet_c1930, FALSE)),
         is_naacp = as.integer(replace_na(is_naacp, FALSE)))

est_df <- est_df |>
  group_by(county_geoid_c1930) |>
  mutate(
    share_blackvet = mean(vet_combined),
  ) |>
  ungroup()

p <- est_df |>
  filter(!vet_combined) |>
  ggplot(aes(x = share_blackvet, y = as.integer(is_naacp))) +
  stat_binscatter(bins = 20) +
  my_theme() +
  labs(
    x = "Share Black veterans in county",
    y = "Prob. NAACP member\n(among non-veterans)"
  ) +
  xlim(0, .11) +
  ylim(-.005, NA)

p_color <- p + 
  geom_smooth(linewidth = .5, color = "#F8766D", method = "lm", se = FALSE, formula = "y ~ x", fullrange = FALSE)
ggsave(file.path(fig_dir, "color", "binscatter_spillovers.pdf"), plot = p_color, width = 5, height = 3)

p_bw <- p +
  geom_smooth(linewidth = .5, color = "black", method = "lm", se = FALSE, formula = "y ~ x", fullrange = FALSE)
ggsave(file.path(fig_dir, "bw", "binscatter_spillovers.pdf"), plot = p_bw, width = 5, height = 3)
